package com.wx.forum.service.impl;

import com.wx.forum.common.AppResult;
import com.wx.forum.common.ResultCode;
import com.wx.forum.dao.BoardMapper;
import com.wx.forum.exception.ApplicationException;
import com.wx.forum.model.Board;
import com.wx.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;
    /**
     * 查询num个正常板块
     * @param num
     * @return
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        //判空
        if(num == null || num <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询板块
        List<Board> boardList = boardMapper.selectByNum(num);

        return boardList;
    }

    /**
     * 查询所有正常板块
     * @return
     */
    @Override
    public List<Board> selectAllNormol() {
        //查询板块
        List<Board> boardList = boardMapper.selectAllNormal();

        return boardList;
    }
    /**
     * 增加板块帖子数量
     * @param id
     */
    @Override
    public void addOneArticleCountByBoardId(Long id) {
        //参数校验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_COUNT));
        }
        //查找板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        int raw = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(raw != 1){
            log.warn(ResultCode.FAILED_BOARD_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_COUNT));
        }
    }

    @Override
    public void subOneArticleCountByBoardId(Long id) {
        //参数校验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_COUNT));
        }
        //查找板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        //贴子数等于0就不减
        if(board.getArticleCount() > 0)
        updateBoard.setArticleCount(board.getArticleCount() - 1);

        int raw = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(raw != 1){
            log.warn(ResultCode.FAILED_BOARD_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_COUNT));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id == null || id == 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByPrimaryKey(id);
    }
}
